package com.xiaobao.coalproperty.module.user.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.user.model.entity.UserDealEntity;
import com.xiaobao.coalproperty.other.adapter.UserDealAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class UserDealActivity extends BaseActivity {

    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private View headView;

    private UserDealAdapter mUserDealAdapter;
    private int page = 1;

    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("我的交易");

        mUserDealAdapter = new UserDealAdapter(this);

        headView = LayoutInflater.from(this).inflate(R.layout.activity_user_deal, null, false);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mUserDealAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        update();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            page = 1;
            update();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            update();
            mXRecyclerView.loadMoreComplete();
        }
    };


    private void update() {
        OkHttpUtils.post().url(UrlConstant.URL_TRANSACTIONRECORD)
                .params(HttpHelperUtils.getInstance().assetEntrustListParams(page + ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    UserDealEntity entity = new Gson().fromJson(response, UserDealEntity.class);
                    if (entity.getCode() == 0) {
                        if (entity.getData().getList().size() > 0) {
                            if (page == 1) {
                                mUserDealAdapter.setAdapterData(entity.getData().getList());
                            } else {
                                mUserDealAdapter.addAdapterData(entity.getData().getList());
                            }
                            page++;
                        }
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }


}
